package com.bidevalution.business.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.bidevalution.business.impl.BeTechnologyItemServiceImpl.TechType;
import com.bidevalution.business.service.BeQualificateItemService;
import com.bidevalution.business.service.BeSectionInfoService;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.support.util.MapUtil;
import com.bidevalution.support.util.StringUtil;
import com.google.common.collect.Lists;
@Service
public class BeQualificateItemServiceImpl implements BeQualificateItemService{

	@Autowired
	private JdbcTemplateService jdbcTemplateService;
	@Autowired
	private BeSectionInfoService beSectionInfoService;
	/**
	 * qualScoreHyper:细项分上限
	 * qualScoreUnder:细项分下限
	 * <p>Description: </p>
	 * @param sectionId
	 * @param parentId
	 * @param type
	 * @return
	 * @see com.bidevalution.business.service.BeTechnologyItemService#selectTree(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> selectTree(String sectionId, String parentId, String type) {
		List<Map<String, Object>> returnList = Lists.newArrayList();
		List<Map<String, Object>> childList = Lists.newArrayList();
		BeSectionInfo beSectionInfo = beSectionInfoService.getBeSectionInfoById(sectionId);
		List<Map<String, Object>> dataList = jdbcTemplateService.selectForList(
				"select comp_ZHMethodID,comp_Name,comp_Idx,comp_ParentID,comp_ProjectID,comp_ScoreHyper,comp_ScoreUnder from td_composite where comp_ProjectID=:projectId and comp_ParentID=:parentId   order by comp_Idx asc",
				MapUtil.create().putValue("projectId", beSectionInfo.getTdProjectid()).putValue("parentId", parentId).getMap());
		for (Map<String, Object> dataMap : dataList) {
			childList.add(MapUtil.create().putValue("id", dataMap.get("comp_ZHMethodID"))
					.putValue("nodeNumber", dataMap.get("comp_Idx")).putValue("nodeName", dataMap.get("comp_Name"))
					.putValue("qualScoreHyper", dataMap.get("comp_ScoreHyper"))
					.putValue("qualScoreUnder", dataMap.get("comp_ScoreUnder"))
					.putValue("children",
							getChildrenNode(beSectionInfo.getTdProjectid(), StringUtil.toString(dataMap.get("comp_ZHMethodID")), type))
					.getMap());
		}

		returnList.add(MapUtil.create().putValue("children", childList).putValue("id", parentId)
				.putValue("nodeName", TechType.geTechType(type)==null?"资信标评审项目":TechType.geTechType(type).getText()).getMap());

		return returnList;
	}

	/**
	 * 递归算法解析成树形结构
	 *
	 * @param defineId
	 * @param seqId
	 * @return
	 * @author daizhifeng
	 */
	public List<Map<String, Object>> getChildrenNode(String projectId, String seqId, String type) {
		List<Map<String, Object>> returnList = Lists.newArrayList();
		// 遍历子节点
		for (Map<String, Object> dataMap : jdbcTemplateService.selectForList(
				"select comp_ZHMethodID,comp_Name,comp_Idx,comp_ParentID,comp_ProjectID,comp_ScoreHyper,comp_ScoreUnder from td_composite where comp_ProjectID=:projectId and comp_ParentID=:parentId   order by comp_Idx asc",
				MapUtil.create().putValue("projectId", projectId).putValue("parentId", seqId).getMap())) {
			if (!StringUtils.isEmpty(dataMap.get("comp_ZHMethodID"))) {
				returnList.add(MapUtil.create().putValue("id", dataMap.get("comp_Idx"))
						.putValue("nodeNumber", dataMap.get("comp_Idx")).putValue("nodeName", dataMap.get("comp_Name"))
						.putValue("qualScoreHyper", dataMap.get("comp_ScoreHyper"))
						.putValue("qualScoreUnder", dataMap.get("comp_ScoreUnder"))
						.putValue("children",
								getChildrenNode(projectId, StringUtil.toString(dataMap.get("comp_ZHMethodID")), type))
						.getMap());
			}

		}
		return returnList;
	}


}
